﻿

namespace Mulala.Cavan.Domain.MainBoundedContext.MulalaModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Core.Persistent;
    using Mulala.Cavan.Domain.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Core.MyLinq;
    using Mulala.Cavan.Core.Extension;

    public class ConsignSpecification : ISpecification<Consign, ConsignSearch>
    {
        public ConsignSpecification()
        {
            this.SearchParams = new ConsignSearch();
        }

        public ConsignSearch SearchParams
        {
            get;
            set;
        }

        public System.Linq.Expressions.Expression<Func<Consign, bool>> Expression
        {
            get
            {
                IQueryBuilder<Consign> queryBuilder = QueryBuilder.Create<Consign>();

                if (null != this.SearchParams)
                {
                    if (null != this.SearchParams.Type)
                    {
                        queryBuilder = queryBuilder.Equals(c => (int)c.Type, this.SearchParams.Type.Value.GetHashCode());
                    }
                    if (this.SearchParams.Experience > 0)
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Experience, this.SearchParams.Experience);
                    }
                    if (this.SearchParams.AreaCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Area.ID.Substring(1, this.SearchParams.AreaCode.Length), this.SearchParams.AreaCode);
                    }
                    if (this.SearchParams.Age > 0)
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Age, this.SearchParams.Age);
                    }
                    if (this.SearchParams.Salary > 0)
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Salary, this.SearchParams.Salary);
                    }
                    if (this.SearchParams.Degree > 0)
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Degree, this.SearchParams.Degree);
                    }
                    if (null != this.SearchParams.SkillPosition)
                    {
                        foreach (int sp in this.SearchParams.SkillPosition)
                        {
                            queryBuilder = queryBuilder.Greater(c => c.NecessarySkill & (long)Math.Pow(2, sp), 0);
                        }
                    }
                    if (this.SearchParams.IsSystemRecommed.HasValue)
                    {
                        queryBuilder = queryBuilder.Greater(c => c.SystemRecommendTimeout, DateTime.Now);
                    }
                    if (this.SearchParams.ConsignStatus.HasValue)
                    {
                        queryBuilder = queryBuilder.Equals(c => (int)c.ConsignStatus, SearchParams.ConsignStatus.Value.GetHashCode());
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }
    }
}
